package com.ws.controller;

import com.sun.javafx.collections.ImmutableObservableList;
import com.ws.bean.Note;
import com.ws.service.NoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class NoteController {

    @Autowired
    private NoteService noteService;


    @RequestMapping("/findNoteByPage")
    public String findNoteByPage(int page, int size, int position, int eid, int did, int status, Model model) {
        //获取参数
        System.out.println("findNoteByPage：获取参数：");
        System.out.println(String.format("page=%d, size=%d, position=%d, eid=%d, did=%d, status=%d", page, size, position, eid, did, status));

        //调用 Service 层
        List<Note> notes = noteService.findNoteByPage(page, size, position, eid, did, status);

        //获取数据总行数
        //position用来查询是老板还是经理，status = -1则是查询自己的请假 否则 用来查是待审批还是已审批
        int lines = noteService.lines(position, status, eid, did);

        //计算总页数
        int pages = lines % size == 0 ? lines / size: lines / size + 1;

        //共享数据
        model.addAttribute("notes", notes);
        model.addAttribute("status", status); //用在是否有操作时
        model.addAttribute("pages", pages);
        model.addAttribute("lines", lines);
        model.addAttribute("page", page);//分享当前所在页
        model.addAttribute("position", position);//用在分页查询

        //响应
        return "leave.jsp";
    }


//    @RequestMapping("/findNoteByPage")
//    public String findNoteByPage(int page, int size,
//                                 @RequestParam(required = false, defaultValue = "0") int eid,
//                                 @RequestParam(required = false, defaultValue = "-1") int position,
//                                 @RequestParam(required = false, defaultValue = "-1") int status,
//                                 @RequestParam(required = false, defaultValue = "-1") int did,Model model) {
//        //1 获取参数
//        //2 调用service
//
//        //查询总行数
//        //查询老板对应待审批的请假记录的总行数
//        //查询经理/普通员工的请假记录的总行数
//        int lines = 0;
//        List<Note> notes = null;
//
//        if(eid==0) {//查询老板待审批的记录和已审批的请假记录
//             System.out.println("did:"+did);
//             notes = noteService.findNoteByPage(page, size, position, status);
//             model.addAttribute("operate", "true");
//             //标志需要操作，待审批
//            lines = noteService.lines(position, status);
//
//        } else {//查询自己的请假记录
//
//            if(did != -1) {//发送了did,代表查询的是经理的待审批的请假记录
//                //查询经理的待审批的记录
//                System.out.println("查询经理的待审批的记录");
//                //page=1&size=4&position=1&status=0&did=?
//                System.out.println(did);
//                notes = noteService.findNoteByPage2(page, size, status, did);
//                model.addAttribute("operate", "true");//标志需要操作，待审批
//                lines = noteService.lines2(status, did);//计算某个部门所有普通员工请假记录的总行数
//
//            } else {//查询经理或员工自己的请假记录
//                //查询经理或员工自己的请假记录
//                notes = noteService.findNoteByPage(page, size, eid);
//                model.addAttribute("operate", "false");//标志查询自己的请假记录和已审批
//                lines = noteService.lines1(eid);
//            }
//        }
//
//        //计算总页数
//        int pages = lines % size == 0 ? lines / size: lines / size + 1;
//
//        //3 共享数据
//        model.addAttribute("notes", notes);
//        model.addAttribute("pages", pages);
//        model.addAttribute("lines", lines);
//        model.addAttribute("page", page);
//
//        System.out.println("查询请假记录：");
//        System.out.println(notes);
//
//        return "leave.jsp";
//    }

    @RequestMapping("/addNote")
    public String addNote(Note note) {
        //1.获取参数
        System.out.println("addNote请求响应时：");
        System.out.println(note);
        //2.调用Service
        noteService.addNote(note);
        //3.共享数据
        //4.响应
        return "findNoteByPage?page=1&size=4&position=" + note.getPosition() +"&eid=" + note.getEid() + "&did=-1&status=-1";
    }

    @RequestMapping("/updateStatus")
    public String updateStatus(int nid, int position, int did, int status) {
        //调用Service层
        System.out.println("updateStatus 获取参数：");
        System.out.println(String.format("nid=%d, position=%d, did=%d, status=%d", nid, position, did, status));
        noteService.updateStatus(nid, status);
        //响应
        return "findNoteByPage?page=1&size=4&position="+position+"&eid=-1&did="+did+"&status=0";
    }

}
